<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="success" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<el-table :data="adoptOrders" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="digest" label="名称" width="260" sortable>
			</el-table-column>
			<el-table-column prop="price" label="售价" width="130" sortable>
			</el-table-column>
			<el-table-column prop="orderSn" label="订单号" width="100" sortable>
			</el-table-column>
			<!--:formatter="formatState"-->
			<el-table-column prop="state" label="状态" width="120"  sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state==0">下架</span>
					<span style="color: red" v-else-if="scope.row.state==1">上架</span>
					<span style="color: darkblue" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="info(scope.$index, scope.row)">详情</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<!--如果没有选中就不能使用:disabled="this.sels.length===0"-->
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!-- 详情 -->
		<el-dialog title="订单详情" :visible.sync="adoptOrderInfoVisible" :close-on-click-modal="false">
			<el-form :model="adoptOrderInfo" label-width="120px" ref="adoptOrderInfo">
				<el-form-item label="摘要" prop="digest">
					<el-input readonly="readonly" v-model="adoptOrderInfo.digest" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="订单号" prop="orderSn">
					<el-input v-model="adoptOrderInfo.orderSn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="price">
					<el-input v-model="adoptOrderInfo.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="state">
					<template scope="scope">
						<span style="color: green" v-if="adoptOrderInfo.state==0">待支付</span>
						<span style="color: red" v-else-if="adoptOrderInfo.state==1">已完成</span>
						<span style="color: gray" v-else>未知</span>
					</template>
				</el-form-item>
				<el-form-item label="最后确认时间" prop="lastConfirmTime">
					<el-input v-model="adoptOrderInfo.lastConfirmTime" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="宠物名称" prop="manager">
					<el-input readonly="readonly" v-model="adoptOrderInfo.pet.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="领养人" prop="parent">
					<el-input readonly="readonly" v-model="adoptOrderInfo.user.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="门店" prop="parent">
					<el-input v-if="adoptOrderInfo.shop" readonly="readonly" v-model="adoptOrderInfo.shop.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="adoptOrderInfoVisible = false">取消</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
//引入  vue-quill-editor
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import quilConfig from "../../common/js/quill-config"

	export default {
        components: {
            quillEditor
        },
		data() {
			return {
			    quillOption:quilConfig,
				filters: {
					keyword: ''
				},
                adoptOrderInfoVisible: false,//订单详情
				adoptOrders: [],
                fileList:[],
				total: 0,
				page: 1,
				pageSize:10,
				listLoading: false,
				sels: [],//列表选中列

				adoptOrderFormVisible: false,//新增界面是否显示
                adoptOrderLoading: false,
                adoptOrderFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
				title:"",
                adoptOrderForm: {
                    id:null,
					name: '',
                    resources: '',
                    saleprice: 0,
                    costprice: 0,
                    detail:{
                        id:null,
                        intro:"",
						orderNotice:""
					}
				},
                adoptOrderInfo:{
                    digest: '',
                    state: '',
                    price: 0,
                    orderSn: null,
                    paySn: null,
                    lastPayTime:'',
                    lastConfirmTime: '',
                    pet: {},
                    user: {},
                    shop: {}
                }
			}
		},
		methods: {

            handleSuccess(response, file, fileList){
                console.log(fileList);
                let resourceTemp = "";
                for(let i=0;i<fileList.length;i++){
					//如果是数组的最后一个元素，不要加逗号了
					if(i == fileList.length -1){
                        resourceTemp = resourceTemp + fileList[i].response.resultObj;
					}else{//如果不是最后一个 拼接上逗号
                        resourceTemp = resourceTemp + fileList[i].response.resultObj + ",";
					}
				}
                this.adoptOrderForm.resources = resourceTemp;
            },
            handleRemove(file, fileList) {
                var filePath =file.response.resultObj;
                this.$http.delete("/fastDfs/?path="+filePath)
                    .then(res=>{
                        if(res.data.success){
                            this.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                            let resourceTemp = "";
                            for(let i=0;i<fileList.length;i++){
                                //如果是数组的最后一个元素，不要加逗号了
                                if(i == fileList.length -1){
                                    resourceTemp = resourceTemp + fileList[i].response.resultObj;
                                }else{//如果不是最后一个 拼接上逗号
                                    resourceTemp = resourceTemp + fileList[i].response.resultObj + ",";
                                }
                            }
                            this.adoptOrderForm.resources = resourceTemp;
                        }else{
                            this.$message({
                                message: '删除失败!',
                                type: 'error'
                            });
                        }
                    })
            },
            handlePreview(file) {
                console.log(file);
            },
            search() {
                // 查询完，原来分页条件已经没用了，要跑到第一页去
                this.page = 1;
                this.getAdoptOrders();
            }
			,
			handleCurrentChange(val) {
				this.page = val;
				this.getAdoptOrders();
			},
			//获取领养订单列表
			getAdoptOrders() {
				let para = {
					currentPage: this.page,
					pageSize:this.pageSize,
					keyword: this.filters.keyword
				};
				this.listLoading = true;
				this.$http.post("/adoptOrder/admin",para)
					.then(result=>{
					    result = result.data; //取出data这一层
                        this.total = result.totalCount;
                        this.adoptOrders = result.data;
                        this.listLoading = false;
					})
					.catch(result=>{
                        this.$message({
                            message: '系统错误',
                            type: 'error'
                        });
					})
			},
			//显示详情界面
            info(index, row){
                this.adoptOrderInfoVisible = true;//显示编辑窗口
                let orderId = row.id;
                this.$http.get("/adoptOrder/"+orderId)
                    .then(result=>{
                        this.adoptOrderInfo = result.data;
                    })
                    .catch(result=>{

                    })
            },
			//显示新增界面
			handleAdd: function () {
				alert("新增等待开发")
			},
			//保存
			save: function () {
                // 表单上定义了ref="adoptOrderForm"
				this.$refs.adoptOrderForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认保存吗？', '温馨提示', {}).then(() => {
							this.adoptOrderLoading = true;
							//拷贝一份新数据
							let para = Object.assign({}, this.adoptOrderForm);
							this.$http.put("/adoptOrder",para)
								.then(result => {
									result = result.data
									//去除忙等框
									this.adoptOrderLoading = false;
									//做提示
									if(result.success){
										this.$message({
											message: '保存成功',
											type: 'success'
										});
									}else{
										this.$message({
											message: result.message,
											type: 'error'
										});
									}
									//关闭对话框
									this.adoptOrderFormVisible =false;
									//重新查询数据
									this.getAdoptOrders();
								})
								.catch(result=>{
									alert("系统错误！");
                                    this.adoptOrderLoading = false;
								})
							;
						});
					}
				});
			},
			//所有选中对象都会传进来
			selsChange: function (sels) {
				this.sels = sels;
				console.log(this.sels)
			},
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let url = "/adoptOrder/"+row.id;
                    this.$http.delete(url)
                        .then(result=>{
                                result = result.data; //{sucess:false,message}
                                //去除忙等框
                                this.listLoading = false;

                                if(result.success){
                                    this.page = 1;
                                    //提示
                                    this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                    //重新查询数据
                                    this.getAdoptOrders();
                                }else{
                                    //提示
                                    this.$message({
                                        message: result.message,
                                        type: 'error'
                                    });
                                    //重新查询数据
                                    this.getAdoptOrders();
                                }
                            }

                        )
                        .catch(result=>{
                            //去除忙等框
                            this.listLoading = false;
                            //提示
                            this.$message({
                                message: '系统错误！',
                                type: 'error'
                            });
                        })

                }).catch(() => {

                });
            },
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id); //[1,2,3]
				console.log(ids);
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//传递数组参数
					this.$http.patch("/adoptOrder",ids)
						.then(result=>{
                            result = result.data;
                            this.listLoading = false;
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
							}else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
							}
							this.page =1; //删除后跳转第一页
                            this.getAdoptOrders();
                        })
						.catch(result=>{
						    alert("系统错误")
						})
				}).catch(() => {

				});
			}

		},
		mounted() {
			this.getAdoptOrders();
		}
	}

</script>

<style scoped>

</style>